const NATION_MAP = {
    '1': '汉族',
    '2': '壮族',
    '3': '满族',
    '4': '回族',
    '5': '苗族',
    '6': '维吾尔族',
    '7': '土家族',
    '8': '彝族',
    '9': '蒙古族',
    '10': '藏族',
    '11': '布依族',
    '12': '侗族',
    '13': '瑶族',
    '14': '朝鲜族',
    '15': '白族',
    '16': '哈尼族',
    '17': '哈萨克族',
    '18': '黎族',
    '19': '傣族',
    '20': '畲族',
    '21': '傈僳族',
    '22': '仡佬族',
    '23': '东乡族',
    '24': '高山族',
    '25': '拉祜族',
    '26': '水族',
    '27': '佤族',
    '28': '纳西族',
    '29': '羌族',
    '30': '土族',
    '31': '仫佬族',
    '32': '锡伯族',
    '33': '柯尔克孜族',
    '34': '达斡尔族',
    '35': '景颇族',
    '36': '毛南族',
    '37': '撒拉族',
    '38': '塔吉克族',
    '39': '阿昌族',
    '40': '普米族',
    '41': '鄂温克族',
    '42': '怒族',
    '43': '京族',
    '44': '基诺族',
    '45': '德昂族',
    '46': '保安族',
    '47': '俄罗斯族',
    '48': '裕固族',
    '49': '乌兹别克族',
    '50': '门巴族',
    '51': '鄂伦春族',
    '52': '独龙族',
    '53': '塔塔尔族',
    '54': '赫哲族',
    '55': '珞巴族',
    '56': '布朗族',
    '57': '其他',
};

const vm = new Vue({
    el: '#main',
    data: {
        show: {
            add: !!(fxj.getSearch().get('type')),
        },
        guysList: [],
        selectedGuyList: [],
    },
    methods: {
        selectGuy,
        submit,
        modifyGuys,
    },
});

function apiready() {
    fxj.fixLayout();
    reqCheckInPersonList();
}

async function reqCheckInPersonList() {
    const close = fxj.dialog.loading({
        noBg: true
    });
    const res = await ajax({
        url: 'home/order/getUserResident',
        type: 'POST',
    });
    close();
    forEach(res.data, guy => {
        guy.nation = NATION_MAP[guy.nation_id];
        guy.selected = false;
    });
    vm.guysList = res.data;
}

function submit() {
    const data = [];
    vm.selectedGuyList.forEach(guy => {
        data.push(guy);
    });
    localStorage.setItem('guysIdList', JSON.stringify(data));
    history.back();
}

function selectGuy(guy) {
    if (guy.selected) {
        const index = vm.selectedGuyList.indexOf(guy);
        guy.selected = false;
        vm.selectedGuyList.splice(index, 1);
    } else {
        guy.selected = true;
        vm.selectedGuyList.push(guy);
    }
}

function back() {
    if (vm.show.add) {
        history.back();
    } else {
        fxj.closeWin();
    }
}

function modifyGuys(id, gid) {
    go('edit_check_in_person.html', {
        data: {
            id,
            gid,
        },
    });
}